package com.huayue.dao;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.huayue.entity.SaleOrder;
import com.huayue.util.PageHelper;
import com.mysql.DBHelper;

/**
 * 销售订单
 * 
 */
@Component
public class SaleOrderDao implements BaseDao<SaleOrder> {
    /**
     * 添加方法
     *
     * @param entity
     * @return
     */
    @Override
    public int insert(SaleOrder entity) {
        return DBHelper.executeUpdate(
            "insert into sale_order(order_number,employee_id,number,total_price,remark) " + "values(?,?,?,?,?)",
            entity.getOrderNumber(), entity.getEmployeeId(), entity.getNumber(), entity.getTotalPrice(),
            entity.getRemark());
    }

    /**
     * 修改方法
     *
     * @param entity
     * @return
     */
    @Override
    public int update(SaleOrder entity) {
        return 0;
    }

    /**
     * 删除方法
     *
     * @param entity
     * @return
     */
    @Override
    public int delete(SaleOrder entity) {
        return DBHelper.executeUpdate("update sale_order set is_status=1 where id=?", entity.getId());
    }

    /**
     * 查询方法 传入可变参数
     *
     * @param obj
     * @return
     */
    @Override
    public List<Map<String, Object>> findAll(Object... obj) {
        return null;
    }

    /**
     * 分页查询方法 传入分页对象和可变参数 分页对象用于分页查询 可变参数用于增加条件
     *
     * @param page
     * @param obj
     * @return
     */
    @Override
    public List<Map<String, Object>> findPage(PageHelper page, Object... obj) {
        String sql = "select so.*,e.realname ename from sale_order so left join employee e on so.employee_id=e.id";
        sql += " where so.is_deleted=0 ";
        SaleOrder saleOrder = (SaleOrder)obj[0];
        if (saleOrder.getOrderNumber() != null && !"".equals(saleOrder.getOrderNumber())) {
            sql += " and concat(e.realname,so.order_number) like '%" + saleOrder.getOrderNumber() + "%' ";
        }
        return DBHelper.executeQuery(sql + "  order by id desc limit ?,?", page.getBegin(), page.getPageSize());
    }

    /**
     * 查询一条数据 通常对象中只需存有id
     *
     * @param entity
     * @return
     */
    @Override
    public Map<String, Object> findOne(SaleOrder entity) {
        return DBHelper.executeQuery("select * from sale_order where id=?", entity.getId()).get(0);
    }

    /**
     * 查询总条数 传入可变参数，用于增加条件
     *
     * @param obj
     * @return
     */
    @Override
    public int count(Object... obj) {
        String sql = "select count(*) counts from sale_order so left join employee e on so.employee_id=e.id";
        sql += " where so.is_deleted=0 ";
        SaleOrder saleOrder = (SaleOrder)obj[0];
        if (saleOrder.getOrderNumber() != null && !"".equals(saleOrder.getOrderNumber())) {
            sql += " and concat(e.realname,so.order_number) like '%" + saleOrder.getOrderNumber() + "%' ";
        }
        return Integer.valueOf(DBHelper.executeQuery(sql).get(0).get("counts").toString());
    }

    public int findMaxId() {
        return Integer.valueOf(DBHelper.executeQuery("select max(id) id from sale_order").get(0).get("id").toString());
    }
}
